package cn.liumouren.jzxd.common.handler;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONUtil;
import cn.liumouren.jzxd.entity.dto.Play;
import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;
import org.springframework.stereotype.Component;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

/**
 * @author Daniel Liu
 * @date 2021/1/26 21:06
 */
@Component
public class ListPlayTypeHandler extends BaseTypeHandler<List<Play>> {
    @Override
    public void setNonNullParameter(PreparedStatement ps, int i, List<Play> parameter, JdbcType jdbcType) throws SQLException {
        ps.setString(i, JSONUtil.toJsonStr(parameter));
    }

    @Override
    public List<Play> getNullableResult(ResultSet rs, String columnName) throws SQLException {
        String jsonStr = rs.getString(columnName);
        JSONArray objects = JSONUtil.parseArray(jsonStr);
        return objects.toList(Play.class);
    }

    @Override
    public List<Play> getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
        String jsonStr = rs.getString(columnIndex);
        JSONArray objects = JSONUtil.parseArray(jsonStr);
        return objects.toList(Play.class);
    }

    @Override
    public List<Play> getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
        String jsonStr = cs.getString(columnIndex);
        JSONArray objects = JSONUtil.parseArray(jsonStr);
        return objects.toList(Play.class);
    }
}
